<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .box1 {
        width: 100px;
        height: 100px;
        background-color: bisque;
        position: absolute;
        left: 0px;
        top: 0px
    }

    .box2 {
        width: 100px;
        height: 100px;
        background-color: aqua;
        position: absolute;
        left: 300px;
    }
</style>
<body>
    <div class="box1"></div>
    <div class="box2"></div>

    <script>
        var box1 =document.querySelector('.box1')
        var box2 =document.querySelector('.box2')
        class Drag {
            constructor(box){
                this.box = box
                this.downFN()
                this.upFN()
            }
            downFN(){
                var that = this
                this.box.onmousedown = function(e){
                    let x = e.offsetX
                    let y = e.offsetY
                    console.log(x,y)
                    that.moveFn(x,y)
                }
            }

            moveFn(x,y){
                let box = this.box
                document.onmousemove = function(e){
                    let left = e.clientX - x
                    let top  = e.clientY - y
                    box.style.left = left + 'px'
                    box.style.top = top + 'px' 
                }
            }
            
            upFN(){
                this.box.onmouseup = function(){
                    document.onmousemove = null
                }
            }
        }
        var  drag1 = new Drag(box1)

       class sonDrag extends Drag{
            moveFn(x,y){
                let box = this.box
                document.onmousemove = function(e){
                    let left = e.clientX - x
                    let top  = e.clientY - y
                    if(left<0){
                        left=0
                    }else if(left>window.ineerWidth-box.clientWidth){
                        left=window.ineerWidth-box.clientWidth
                    }
                    if(top<0){
                        top = 0
                    }else if (top>window.innerHeight-box.offsetHeight){
                        top = window.innerHeight-box.offsetHeight
                    }
                    box.style.left = left + 'px'
                    box.style.top = top + 'px' 
                }
            }
       }
       var  drag1 = new sonDrag(box2)
    </script>
</body>
</html>